import random
rnd = random.random
def run(ObjList,G,T,times):
    """运行函数"""
    for time in range(0,times):
        for i in ObjList:
            a = [0, 0, 0] # 加速度
            for j in ObjList:
                if i==j :
                    continue
                r_2 = (j.coor[0]-i.coor[0])**2 + (j.coor[1]-i.coor[1])**2 + (j.coor[2]-i.coor[2])**2
                for x in range(0,3):
                    a[x] += G * j.mass * (j.coor[x] - i.coor[x]) / (r_2 ** 1.5)
            for x in range(0,3):
                i.speed[x] += a[x]*T
                i.coor[x]  += i.speed[x]*T
    print("[End] 运行完成","\n",ObjList)

class Body:
    """物体对象，表示为一个物体
    定义方法
    =======
    Body(name, mass=random, coor=random, speed=random)
    属性
    =======
        speed	:[x,y,z]
        coor	:[x,y,z]
        mass	:mass
        name	:string
    """
    def __init__(self,name,mass=0,coor=0,speed=0):
        self.name = name
        self.coor = coor
        self.speed = speed
        self.mass = mass
        if mass == 0: self.mass = rnd() * 10
        if coor == 0: self.coor = [(rnd()-.5)*20,(rnd()-.5)*20,(rnd()-.5)*20,]
        if speed== 0: self.speed= [(rnd()-.5)*20,(rnd()-.5)*20,(rnd()-.5)*20,]
    def __repr__(self):
        return "".join(["[Body] ",str(self.name),
                        "\n参数:",
                        "\n质量 ",str(self.mass),
                        "\n坐标 ",str(self.coor),
                        "\n速度 ",str(self.speed)])
    def runWith(self,other,G=10.0,T=0.00001,times=1000):
        """将该天体与给定天体在万有引力作用下运行"""
        if other == () or other == []:
            raise ValueError("[Error] 未给定运行的天体")
        run([self] + list(other),G,T,times)
